//
//  CLOriginalView.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/13.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class CLOriginalView: UIView {

    //记录原创微博底部的约束
    var originalBottom:Constraint?
    var statusViewModel:CLStatusViewModel?{
        didSet {
          //判断url  是否为正确的地址
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                headImageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //为昵称赋值
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //发布时间
            timeLabel.text = statusViewModel?.time
            //来源
            sourceLabel.text = statusViewModel?.sourceContent
            //微博内容
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedStr
            //认证等级
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            //会员等级
            mbrankImageView.image = statusViewModel?.mbrankImage
            //卸载约束
            originalBottom?.uninstall()
            //判断是否有配图
            if let count = statusViewModel?.status?.pic_urls?.count where count > 0 {
                //有配图
                pictureView.hidden = false
                pictureView.picUrl = statusViewModel?.status?.pic_urls
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
            }else {
                //没有配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
    }

    
    // MARK:  懒加载控件
    //头像
    private lazy var headImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级图片
    private lazy var verifiedTypeImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //会员等级
    private lazy var mbrankImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //微博发送时间
    private lazy var timeLabel:UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //微博发送来源
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.blueColor(), fontSize: 12)
    //微博内容
    private lazy var contentLabel:UILabel = {
       let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        //自动换行
        label.numberOfLines = 0
        return label
    }()
    //微博配图
    private lazy var pictureView:CLStatusPictureView = {
        let view = CLStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    //自定义布局
    override init(frame: CGRect) {
        super.init(frame:frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置布局
    private func setUpUI(){
        
        //添加视图
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置约束
        //头像
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //认证等级
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //昵称
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(headImageView)
        }
        //vip
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(screenNameLabel)
            
        }
        //微博时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView.snp_bottom)
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
        }
        //微博来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        //微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset( HomeCellMargin)
            make.leading.equalTo( contentLabel)
        }
        //原创微博底部的约束
        self.snp_makeConstraints { (make) -> Void in
             self.originalBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
